package cn.wolfcode.wolf2w.cloud.msg.feign;

import cn.wolfcode.wolf2w.cloud.msg.service.ISmsRedisService;
import cn.wolfode.wolf2w.cloud.commom.util.Consts;
import cn.wolfode.wolf2w.cloud.commom.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class SmsFeignClient implements ISmsFeignApi{
//写真正的逻辑
    @Autowired
    private ISmsRedisService smsRedisService;
    @Override
    public JsonResult sendVerifyCode(String phone) {
        //1创建验证码，编辑短信
        String code = UUID.randomUUID().toString().
                replaceAll("-", "").substring(0, 4);

        StringBuilder sb = new StringBuilder(80);
        sb.append("您注册验证码:").append(code).append(", 请求在").
                append(Consts.VERIFY_CODE_VAI_TIME).append("分钟之内使用");

       //2发生短信：简单就是怎么让java发起http请求
        System.out.println(sb.toString());//将短信验证码存入redis
        smsRedisService.setVerifyCode(phone, code);

        return JsonResult.success();
    }

    @Override
    public JsonResult checkVerifyCode(String phone, String verifyCode) {
//拿到验证
        String code = smsRedisService.getVerifyCode(phone);
        //校验两个是否相等
        if(code == null || !code.equalsIgnoreCase(verifyCode)){
            throw new RuntimeException("验证码失效或错误");
        }
        return JsonResult.success(true);
    }
}
